package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.modules.hos.entity.MedicineEntity;
import io.renren.modules.hos.entity.ShopCarEntity;
import io.renren.modules.hos.service.MedicineService;
import io.renren.modules.hos.service.ShopCarService;
import io.renren.modules.hos.vo.MedicineVo;
import io.renren.modules.hos.vo.OrderMedicineVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.OrderMedicineDao;
import io.renren.modules.hos.entity.OrderMedicineEntity;
import io.renren.modules.hos.service.OrderMedicineService;


@Service("orderMedicineService")
public class OrderMedicineServiceImpl extends ServiceImpl<OrderMedicineDao, OrderMedicineEntity> implements OrderMedicineService {
    @Autowired
    private MedicineService medicineService;
    @Autowired
    private ShopCarService shopCarService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderMedicineEntity> page = this.page(
                new Query<OrderMedicineEntity>().getPage(params),
                new QueryWrapper<OrderMedicineEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<OrderMedicineVo> queryByOrderNum(Long orderId) {
        LambdaQueryWrapper<OrderMedicineEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderMedicineEntity::getOrderId, orderId);
        List<OrderMedicineEntity> orderMedicineEntities = baseMapper.selectList(queryWrapper);

        return copyList(orderMedicineEntities);
    }

    @Override
    public void saveShopCar(List<ShopCarEntity> shopCarEntities, Long orderId) {
        for (ShopCarEntity shopCarEntity : shopCarEntities) {
            insertOrderMedicine(shopCarEntity, orderId);
        }
    }

    public OrderMedicineVo copy(OrderMedicineEntity orderMedicineEntity){
        MedicineEntity medicineEntity = medicineService.getById(orderMedicineEntity.getMedicineId());
        OrderMedicineVo OrderMedicineVo = new OrderMedicineVo();
        BeanUtils.copyProperties(orderMedicineEntity, OrderMedicineVo);
        OrderMedicineVo.setMedicineName(medicineEntity.getMedicineName());
        OrderMedicineVo.setMedicineImg(medicineEntity.getMedicineImg());

        return OrderMedicineVo;
    }

    public List<OrderMedicineVo> copyList(List<OrderMedicineEntity> orderMedicineEntities){
        List<OrderMedicineVo> orderMedicineVos = new ArrayList<>();
        for (OrderMedicineEntity orderMedicineEntity : orderMedicineEntities) {
            orderMedicineVos.add(copy(orderMedicineEntity));
        }

        return orderMedicineVos;
    }

    public void insertOrderMedicine(ShopCarEntity shopCarEntity, Long orderId){
        MedicineEntity medicineEntity = medicineService.getById(shopCarEntity.getMedicineId());

        OrderMedicineEntity orderMedicineEntity = new OrderMedicineEntity();
        orderMedicineEntity.setMedicineId(shopCarEntity.getMedicineId());
        orderMedicineEntity.setOrderId(orderId);
        orderMedicineEntity.setMedicineNum(shopCarEntity.getMedicineNum());
        orderMedicineEntity.setMedicinePrice(medicineEntity.getMedicinePrice() * shopCarEntity.getMedicineNum());
        orderMedicineEntity.setStatus(0);
        orderMedicineEntity.setCreateTime(new Date());
        this.save(orderMedicineEntity);

        //将购物车物品状态修改
        shopCarEntity.setStatus(1);
        shopCarService.updateById(shopCarEntity);
    }

}